package com.eccg.shopping.activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eccg.shopping.R;

public abstract class BaseActivity extends Activity {
	protected final static int DAOGOUINDEX = 0;
	protected final static int DIANPINGINDEX = 1;
	protected final static int QUCHEINDEX = 2;
	protected final static int XUEPININDEX = 3;
	protected final static int MOREINDEX = 4;
	protected abstract int getMenuIndex();
	public static final int Horizontal = 0;
	public static final int Vertical = 1;
	private static DisplayMetrics dm = null;
	
	protected LinearLayout body;
	protected LinearLayout title;
	protected LinearLayout centerTitle;
	protected TextView titleTx;
	protected LinearLayout leftTitle;
	protected LinearLayout rightTitle;
	protected LinearLayout bottom;
	private static final int[] srcIds = new int[] {R.drawable.daogou_menu_bg,R.drawable.dianping_menu_bg,R.drawable.xunche_menu_bg,R.drawable.xuepin_menu_bg,R.drawable.more_menu_bg};
	private static final int[] selectSrcIds = new int[] {R.drawable.btn_index_tap,R.drawable.btn_comment_tap,R.drawable.btn_park_tap,R.drawable.btn_shopping_tap,R.drawable.btn_more_tap};; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.base_layout);
		body = (LinearLayout) findViewById(R.base.body);
		bottom = (LinearLayout) findViewById(R.base.bottom);
		title = (LinearLayout) findViewById(R.base.title);
		initHeader();
		initBottom();
	}
	
	protected void initHeader(){
		rightTitle = (LinearLayout) findViewById(R.base.right_title);
		centerTitle = (LinearLayout) findViewById(R.base.center_title);
		leftTitle = (LinearLayout) findViewById(R.base.left_title);
		titleTx = (TextView) findViewById(R.title_main.title);
		title.getLayoutParams().height = getPixbyPercent(0.091633, Vertical);
		leftTitle.setLayoutParams(new LinearLayout.LayoutParams(
				(getPixbyPercent(2.0 / 10.0, Horizontal)), LayoutParams.FILL_PARENT));
		centerTitle.setLayoutParams(new LinearLayout.LayoutParams(
				(getPixbyPercent(6.0 / 10.0, Horizontal)), LayoutParams.FILL_PARENT));
		rightTitle.setLayoutParams(new LinearLayout.LayoutParams(
				(getPixbyPercent(2.0 / 10.0, Horizontal)), LayoutParams.FILL_PARENT));
	}
	
	protected void initBottom() {
		bottom.getLayoutParams().height = getPixbyPercent(0.1, Vertical);
		Button daogouMenu = (Button) findViewById(R.base.daogouMenu);
		Button dianpingMenu = (Button) findViewById(R.base.dianpingMenu);
		Button xuepinMenu = (Button) findViewById(R.base.xuepinMenu);
		Button qucheMenu = (Button) findViewById(R.base.xuncheMenu);
		Button moreMenu = (Button) findViewById(R.base.moreMenu);
		
		setMenuItem(daogouMenu, DAOGOUINDEX);
		setMenuItem(dianpingMenu, DIANPINGINDEX);
		setMenuItem(xuepinMenu, XUEPININDEX);
		setMenuItem(qucheMenu, QUCHEINDEX);
		setMenuItem(moreMenu, MOREINDEX);
		
		
		
	}
	
	private void setMenuItem(final Button menuImgView, final int index){
		menuImgView.setBackgroundResource(index==getMenuIndex()? selectSrcIds[index]:srcIds[index]);
		if(index!=getMenuIndex()){
			final Intent intent = new Intent();
			switch (index) {
			case DAOGOUINDEX:
				intent.setClass(this, DaoGouActivity.class);
				break;
			case DIANPINGINDEX:
				intent.setClass(this, DianPingActivity.class);
				break;
			case QUCHEINDEX:
				intent.setClass(this, XunCheActivity.class);
				break;
			case XUEPININDEX:
				intent.setClass(this, XuePinActivity.class);
				break;
			case MOREINDEX:
				intent.setClass(this, GengDuoActivity.class);
				break;
			default:
				break;
			}
			menuImgView.setOnClickListener(new Button.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					menuImgView.setBackgroundResource(selectSrcIds[index]);
					startActivity(intent);
					finish();
				}
			});
		}
		
	}
	
	
	@Override
	public void setContentView(int layoutResID) {
		if (layoutResID == R.layout.base_layout)
			super.setContentView(layoutResID);
		else {
			getLayoutInflater().inflate(layoutResID, body);
		}
	}

	@Override
	public void setContentView(View view, ViewGroup.LayoutParams params) {
		body.removeAllViews();
		body.addView(view, params);
	}
	
	protected int getPixbyPercent(double percent, int orientation){
		if(dm==null){
			dm = new DisplayMetrics();
			getWindowManager().getDefaultDisplay().getMetrics(dm);
		}
		
		int screenW = dm.widthPixels; // 获取分辨率宽度
		int screenH = dm.heightPixels; // 获取分辨率高度
		return Horizontal == orientation ? (int) (screenW * percent)
				: (int) (screenH * percent);
	}
	
	
}
